//
//  ComeStoreViewControl.m
//  ai2ban
//
//  Created by Heartbeats on 2018/1/10.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "ComeStoreViewControl.h"
#import "ComeStoreViewCell.h"
#import "ComeStoreHeadView.h"
#import "ComeStoreGoodsControl.h"

@interface ComeStoreViewControl ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) NSArray<UserModel *> *stores;
@end

@implementation ComeStoreViewControl

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"商家";
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"ComeStoreViewCell" bundle:nil] forCellReuseIdentifier:@"ComeStoreViewCell"];
    [self.view addSubview:self.tableView];
    
    [self getStoreList];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void) getStoreList {
    NSDictionary *param = @{@"offset": @"0", @"limit": @"100"};
    [[NetworkManager shareNetwork] requestWithURLString:@"app/system/getStorelist" parameters:param method:@"" callBack:^(id responseObject) {
        if ([responseObject[@"state"]intValue]) {
            self.stores = [UserModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.tableView reloadData];
        }
        return;
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.stores.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ComeStoreViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ComeStoreViewCell" forIndexPath:indexPath];
    cell.storeModel = self.stores[indexPath.item];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    ComeStoreHeadView *headView = [[ComeStoreHeadView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Height, 44)];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ComeStoreGoodsControl *goodsControl = [[ComeStoreGoodsControl alloc] init];
    goodsControl.storeId = self.stores[indexPath.item].userId;
    [self.navigationController pushViewController:goodsControl animated:true];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
